package com.example.studentdemo.controller;

import com.example.studentdemo.annotation.RequiresRole;
import com.example.studentdemo.common.Result;
import com.example.studentdemo.entity.Subject;
import com.example.studentdemo.constant.RoleConstant;
import com.example.studentdemo.service.SubjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/subject")
@Tag(name = "课程管理", description = "课程相关操作接口")
public class SubjectController {
    @Resource
    private SubjectService subjectService;

    @GetMapping
    @Operation(summary = "查询所有课程", description = "查询所有课程接口")
    @Parameter(description = "查询条件", name = "subject", required = false)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE, RoleConstant.STUDENT_ROLE})
    public Result selectAll(Subject subject) {
        List<Subject> list = subjectService.selectAll(subject);
        return Result.success(list);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询课程", description = "分页查询课程接口")
    @Parameter(description = "页码", name = "pageNum", required = true)
    @Parameter(description = "每页数量", name = "pageSize", required = true)
    @Parameter(description = "查询条件", name = "subject", required = false)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE, RoleConstant.STUDENT_ROLE})
    public Result selectPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, Subject subject) {
        return Result.success(subjectService.selectPage(pageNum, pageSize, subject));
    }

    @PostMapping
    @Operation(summary = "添加课程", description = "添加课程接口")
    @Parameter(description = "课程信息", name = "subject", required = true)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result add(@RequestBody Subject subject) {
        subjectService.add(subject);
        return Result.success();
    }

    @PutMapping
    @Operation(summary = "修改课程", description = "修改课程接口")
    @Parameter(description = "课程信息", name = "subject", required = true)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result update(@RequestBody Subject subject) {
        subjectService.update(subject);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除课程", description = "删除课程接口")
    @Parameter(description = "课程ID", name = "id", required = true)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result deleteById(@PathVariable Integer id) {
        subjectService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除课程", description = "批量删除课程接口")
    @Parameter(description = "课程列表", name = "subjectList", required = true)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result deleteBatch(@RequestBody List<Subject> subjectList) {
        subjectService.deleteBatch(subjectList);
        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询课程", description = "根据ID查询课程接口")
    @Parameter(description = "课程ID", name = "id", required = true)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE, RoleConstant.STUDENT_ROLE})
    public Result selectById(@PathVariable Integer id) {
        return Result.success(subjectService.selectById(id));
    }
}